import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PromotionActivityApi } from '#/api/reservation/promotion-activity';

import { z } from '#/adapter/form';
import { DICT_TYPE, getDictOptions, getRangePickerDefaultProps } from '#/utils';
import { useAccess } from '@vben/access';

const { hasAccessByCodes } = useAccess();

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'activityType',
      label: '活动类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MRS_ACTIVITY_TYPE, 'number'),
        placeholder: '请选择活动类型',
      },
    },
    {
      fieldName: 'minAmount',
      label: '最低触发余额',
      component: 'Input',
      componentProps: {
        placeholder: '请输入最低触发余额',
      },
    },
    {
      fieldName: 'giftType',
      label: '赠送类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MRS_GIFT_TYPE, 'number'),
        placeholder: '请选择赠送类型',
      },
    },
    {
      fieldName: 'giftValue',
      label: '赠送数值',
      component: 'Input',
      componentProps: {
        placeholder: '请输入赠送数值',
      },
    },
    {
      fieldName: 'startTime',
      label: '活动开始时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'endTime',
      label: '活动结束时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'activityType',
      label: '活动类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MRS_ACTIVITY_TYPE, 'number'),
        placeholder: '请选择活动类型',
      },
    },
    {
      fieldName: 'minAmount',
      label: '最低触发余额',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入最低触发余额',
      },
    },
    {
      fieldName: 'giftType',
      label: '赠送类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MRS_GIFT_TYPE, 'number'),
        placeholder: '请选择赠送类型',
      },
    },
    {
      fieldName: 'giftValue',
      label: '赠送数值',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入赠送数值',
      },
    },
    {
      fieldName: 'startTime',
      label: '活动开始时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'endTime',
      label: '活动结束时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(
  onActionClick?: OnActionClickFn<PromotionActivityApi.PromotionActivity>,
): VxeTableGridOptions<PromotionActivityApi.PromotionActivity>['columns'] {
  return [
    {
      field: 'id',
      title: '主键',
      minWidth: 120,
    },
    {
      field: 'activityType',
      title: '活动类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MRS_ACTIVITY_TYPE },
      },
    },
    {
      field: 'minAmount',
      title: '最低触发余额',
      minWidth: 120,
    },
    {
      field: 'giftType',
      title: '赠送类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MRS_GIFT_TYPE },
      },
    },
    {
      field: 'giftValue',
      title: '赠送数值',
      minWidth: 120,
    },
    {
      field: 'startTime',
      title: '活动开始时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'endTime',
      title: '活动结束时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'operation',
      title: '操作',
      minWidth: 200,
      align: 'center',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      cellRender: {
        attrs: {
          nameField: 'id',
          nameTitle: '优惠活动',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: hasAccessByCodes(['reservation:promotion-activity:update']),
          },
          {
            code: 'delete',
            show: hasAccessByCodes(['reservation:promotion-activity:delete']),
          },
        ],
      },
    },
  ];
}

